package com.two.screen.water.balance.permission

import android.Manifest
import android.app.Activity
import android.os.Build
import android.text.TextUtils
import androidx.appcompat.app.AlertDialog
import com.two.screen.water.balance.R
import com.two.screen.water.balance.utils.ToastUtil
import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission


/**
 * 创建者     Noel
 * 创建时间   2018/1/31 15:11
 * 描述	      权限管理
 */
object PermissionUtil {
    const val REQUEST_CODE_SETTING = 1


    var READ_PHONE_STATE =
        // Android 版本大于等于 Android12 时
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            arrayOf(
                /*Permission.READ_PHONE_STATE,*/ Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE,
                Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH_CONNECT
            )
        } else {
            arrayOf(
                /*Permission.READ_PHONE_STATE,*/ Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE,
                Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION
            )
        }

    /**
     * 请求权限
     */
    fun requestPermission(activity: Activity, permissions: Array<String>, success: Runnable, failed: Runnable? = null) {
        AndPermission.with(activity)
            .runtime()
            .permission(permissions)
            .rationale(RuntimeRationale())
            .onGranted { /*ToastUtil.showToast("授权成功");*/success.run() }
            .onDenied { permissions ->
                failed?.run()
                ToastUtil.showToast("授权失败!")
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    showSettingDialog(activity, permissions)
                }
            }
            .start()
    }


    fun requestPermissionForAlertWindow(activity: Activity, success: Runnable, failed: Runnable? = null) {
        AndPermission
            .with(activity)
            .overlay()
            .rationale(OverlayRationale())
            .onGranted { /*ToastUtil.showToast("授权成功")*/;success.run() }
            .onDenied { permissions ->
                failed?.run()
                ToastUtil.showToast("授权失败!")
            }
            .start()
    }

    /**
     * Display setting dialog.
     */
    fun showSettingDialog(activity: Activity, permissions: List<String?>?) {
        val permissionNames = Permission.transformText(activity, permissions)
        val message = activity.getString(R.string.message_permission_always_failed, TextUtils.join("\n", permissionNames))
        AlertDialog.Builder(activity).setCancelable(false)
            .setTitle("提示")
            .setMessage(message)
            .setPositiveButton(R.string.setting) { dialog, which -> setPermission(activity) }
            .setNegativeButton(R.string.cancel) { dialog, which -> }
            .show()
    }

    /**
     * Set permissions.
     */
    private fun setPermission(activity: Activity) {
        AndPermission.with(activity).runtime().setting().start(REQUEST_CODE_SETTING)
    }
}